package servlet;

import entity.Fruit;
import service.FruitService;
import service.impl.FruitServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/FruitServlet")
public class FruitServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        FruitService service = new FruitServiceImpl();
        String opr = "list";
        if (req.getParameter("opr") != null){
            opr = req.getParameter("opr");
        }

        if (opr.equals("list")){
            int pageIndex = 1;
            if (req.getParameter("pageIndex") != null){
                pageIndex = Integer.parseInt(req.getParameter("pageIndex"));
            }
            int pageSize = 3;
            int count = service.count();
//            System.out.println(count);
            int pageCount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
            List<Fruit> list = service.selectAllFruit(pageIndex,pageSize);
            req.setAttribute("list",list);
            req.setAttribute("pageIndex",pageIndex);
            req.setAttribute("pageCount",pageCount);
            req.getRequestDispatcher("show.jsp").forward(req,resp);
        }else if (opr.equals("one")){
            int fruitid = Integer.parseInt(req.getParameter("fruitid"));
            Fruit fruit = service.selectById(fruitid);
            req.setAttribute("fruit",fruit);
            req.getRequestDispatcher("showInfo.jsp").forward(req,resp);
        }else if (opr.equals("del")){
            int fruitid = Integer.parseInt(req.getParameter("fruitid"));
            int r = service.delete(fruitid);
            if (r > 0){
                out.print("true");
            }else {
                out.print("false");
            }
        }else if (opr.equals("add")){
//            int fruitid = Integer.parseInt(req.getParameter("fruitid"));
            String type = req.getParameter("type");
            String breed = req.getParameter("breed");
            String area = req.getParameter("area");
            String brief = req.getParameter("brief");
            int weight = Integer.parseInt(req.getParameter("weight"));
            double price = Double.parseDouble(req.getParameter("price"));
            Fruit fruit = new Fruit(type,breed,area,brief,weight,price);
            int r = service.add(fruit);
            if (r > 0){
                out.print("<script>alert('添加成功！');location.href='FruitServlet';</script>");
            }else {
                out.print("<script>alert('添加失败！');</script>");
            }
        }else if (opr.equals("upd")){
            int fruitid = Integer.parseInt(req.getParameter("fruitid"));
            Fruit fruit = service.selectById(fruitid);

            if (req.getParameter("type") == null){
                req.setAttribute("fruit",fruit);
                req.getRequestDispatcher("update.jsp").forward(req,resp);
            }else {
                String type = req.getParameter("type");
                String breed = req.getParameter("breed");
                String area = req.getParameter("area");
                String brief = req.getParameter("brief");
                int weight = Integer.parseInt(req.getParameter("weight"));
                double price = Double.parseDouble(req.getParameter("price"));
                fruit = new Fruit(fruitid,type,breed,area,brief,weight,price);
                int r = service.update(fruit);
                if (r > 0)
                    out.print("<script>alert('更新成功！');location.href='FruitServlet';</script>");
                else
                    out.print("<script>alert('更新失败！');location.href='FruitServlet';</script>");
            }
        }else if (opr.equals("type")){
            int pageIndex = 1;
            if (req.getParameter("pageIndex") != null){
                pageIndex = Integer.parseInt(req.getParameter("pageIndex"));
            }
            String type = req.getParameter("type");
            int pageSize = 3;
            int count = service.count();
//            System.out.println(count);
            int pageCount = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
            List<Fruit> list = service.selectFruit(pageIndex,pageSize,type);
            req.setAttribute("list",list);
            req.setAttribute("pageIndex",pageIndex);
            req.setAttribute("pageCount",pageCount);
            req.getRequestDispatcher("show.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
